package cn.yy.cloudpay.db;

import org.xutils.DbManager;
import org.xutils.db.sqlite.SqlInfo;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.db.table.DbModel;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import cn.yy.cloudpay.util.db.DbConfig;

/**
 * Created by Test on 2016/12/success.
 */
public class RecordDB {
    /**
     * 存储Parent到数据库
     * @param record 需要存储的对象
     * @return
     */
    public boolean insertRecordModel(RecordModel record) {
        try {
            DbManager dbManager = x.getDb(DbConfig.getDaoConfig());
            return dbManager.saveBindingId(record);
        } catch (DbException e) {
            e.printStackTrace();
        }
        return false;
    }

    public <T> T findByIdRecordModel(Class<T> entityType, Long id){
        try {
            DbManager dbManager = x.getDb(DbConfig.getDaoConfig());
            return dbManager.findById(entityType, id);
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    public RecordModel findRecordModel(Long tracck_id){
        try {
            DbManager dbManager = x.getDb(DbConfig.getDaoConfig());
            RecordModel rcs = dbManager.selector(RecordModel.class)
                    .where("track_id","=",tracck_id).findFirst();
            return rcs;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<DbModel> findDbModelAll(String sql){
        try {
            DbManager dbManager = x.getDb(DbConfig.getDaoConfig());
            return dbManager.findDbModelAll(new SqlInfo(sql));
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<RecordModel> dbFind(){
        List<RecordModel> users = new ArrayList<RecordModel>();
        try {
            DbManager dbManager = x.getDb(DbConfig.getDaoConfig());
            users = dbManager.selector(RecordModel.class)
                .orderBy("create_time",true)
                .limit(Integer.MAX_VALUE) //只查询两条记录
                .findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return users;
    }

    public void deleteByOrderid(String orderid){
        try {
            DbManager dbManager = x.getDb(DbConfig.getDaoConfig());
            WhereBuilder whereBuilder = WhereBuilder.b();
            whereBuilder.and("orderid","=",orderid);
            dbManager.delete(RecordModel.class,whereBuilder);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void deleteById(Long id){
        try {
            DbManager dbManager = x.getDb(DbConfig.getDaoConfig());
           dbManager.deleteById(RecordModel.class,id);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void updateById(RecordModel recordModel){
        try {
            DbManager dbManager = x.getDb(DbConfig.getDaoConfig());
            dbManager.update(recordModel,"photo","remark");
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public long count(){
        try {
            DbManager dbManager = x.getDb(DbConfig.getDaoConfig());
            return dbManager.selector(RecordModel.class).count();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public void deleteTableAllTate(Class entity){
        try {
            DbManager dbManager = x.getDb(DbConfig.getDaoConfig());
            dbManager.delete(entity);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
}
